package cn.lau.pp.module.vr.service.game;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.lau.pp.module.vr.controller.admin.game.vo.*;
import cn.lau.pp.module.vr.dal.dataobject.game.GameDO;
import cn.lau.pp.framework.common.pojo.PageResult;
import cn.lau.pp.framework.common.pojo.PageParam;
import cn.lau.pp.framework.common.util.object.BeanUtils;

import cn.lau.pp.module.vr.dal.mysql.game.GameMapper;

import static cn.lau.pp.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.lau.pp.module.vr.enums.ErrorCodeConstants.*;

/**
 * vr游戏 Service 实现类
 *
 * @author Lau
 */
@Service
@Validated
public class GameServiceImpl implements GameService {

    @Resource
    private GameMapper gameMapper;

    @Override
    public Integer createGame(GameSaveReqVO createReqVO) {
        // 插入
        GameDO game = BeanUtils.toBean(createReqVO, GameDO.class);
        gameMapper.insert(game);
        // 返回
        return game.getId();
    }

    @Override
    public void updateGame(GameSaveReqVO updateReqVO) {
        // 校验存在
        validateGameExists(updateReqVO.getId());
        // 更新
        GameDO updateObj = BeanUtils.toBean(updateReqVO, GameDO.class);
        gameMapper.updateById(updateObj);
    }

    @Override
    public void deleteGame(Integer id) {
        // 校验存在
        validateGameExists(id);
        // 删除
        gameMapper.deleteById(id);
    }

    private void validateGameExists(Integer id) {
        if (gameMapper.selectById(id) == null) {
            throw exception(GAME_NOT_EXISTS);
        }
    }

    @Override
    public GameDO getGame(Integer id) {
        return gameMapper.selectById(id);
    }

    @Override
    public PageResult<GameDO> getGamePage(GamePageReqVO pageReqVO) {
        return gameMapper.selectPage(pageReqVO);
    }

}